package io.gitee.xyuesheng.day08.Thread;

public class TestDeadlock {
    public static void main(String[] args) {
        Object chopstick1 = new Object();
        Object chopstick2 = new Object();

        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (chopstick1) {
                    System.out.println("拿到了第一根筷子");

                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    synchronized (chopstick2) {
                        System.out.println("拿到了第二根筷子");
                    }

                }
            }
        });

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (chopstick2) {
                    System.out.println("拿到了第二根筷子");

                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    synchronized (chopstick1) {
                        System.out.println("拿到了第一根筷子");
                    }

                }
            }
        });

        t1.start();
        t2.start();
    }
}
